# -*-coding:utf-8-*-

# 初始化
dic1 = {'b': 98, 'a': 97, 'c': 99}
dic2 = {i: ord(i) for i in 'bac'}
dic3 = dict(b=98, a=97, c=99)
print(dic1)
print(dic2)
print(dic3)

# 排序
# 方法一: 将字典转换成列表再进行排序
sortlist = list(dic2.items())
sortlist.sort(key=lambda x: x[0])
print(sortlist)
# 方法二: 取出键名或键值进行排序
keys = list(dic1.keys())
keys.sort()
for k in keys:
    print(k+': '+str(dic1[k]))
# 方法三: 使用sorted()能字段对键名进行排序
a = sorted(dic1, key=lambda x: dic1[x])
for k in a:
    print(k+': '+str(dic1[k]))

# 判断是否存在该键名
if 'f' not in dic1:
    print('the key is missing')

# 自定义排序
order = {'A':'1','B':'2'}
l = list(dic.items())
l.sort(key=lambda k: int(''.join(order[i] for i in k[0])))
res = ''.join(i[1] for i in l)
print(res)